package com.token.iot.bratterycharging.boss.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.transaction.OutstandingTransactionCallback;
import com.outstanding.framework.base.transaction.OutstandingTransactionTemplate;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.ReturnCode;
import com.outstanding.framework.core.SysCode;
import com.token.base.order.api.OrderVO;
import com.token.base.order.api.PayInfoDTO;
import com.token.base.order.api.PayItemDTO;
import com.token.iot.bratterycharging.boss.dao.DeviceDAO;
import com.token.iot.bratterycharging.boss.dao.OrderDAO;
import com.token.iot.bratterycharging.boss.dao.PayDAO;
import com.token.iot.bratterycharging.boss.dao.PayItemDAO;
import com.token.iot.bratterycharging.boss.web.Agents;
import com.token.iot.bratterycharging.boss.web.Amount;
import com.token.iot.bratterycharging.boss.web.Merchant;
import com.token.iot.bratterycharging.boss.web.PayDTO;
import com.token.iot.bratterycharging.boss.web.ReqPageDto;
import com.token.iot.bratterycharging.boss.web.ResCode;

@Service
public class OrderServcice {

	@Autowired
	private PayDAO payDao;

	@Autowired
	private PayItemDAO payItemDAO;

	@Autowired
	private OrderDAO orderDao;
	/**
	 * 强制事务（需要转移控制权的）
	 */
	@Autowired
	protected OutstandingTransactionTemplate forceTransactionTemplate;

	/**
	 * 添加订单产品要素
	 * 
	 * @param dtos
	 * @return
	 * @throws PendingException
	 */
	public ReturnCode addPayItem(List<PayItemDTO> dtos) throws PendingException {
		PayDTO paydto = new PayDTO();
		// 有控制权的
		ReturnCode result = forceTransactionTemplate.execute(new OutstandingTransactionCallback<ReturnCode>() {
			// 先调用账户系统，创建对应的代理商账户
			/**
			 * 此方法必须抛出PendingException异常
			 */
			@Override
			public ReturnCode doInTransaction(TransactionStatus status) throws PendingException {
				PayItemDTO first = dtos.get(0);
				if (first.getAgentId() == null) {

					String merchantId = first.getMerchantId();
					paydto.setMerchantId(merchantId);
					payDao.insertMerchant(paydto);

				} else {
					String agentId = first.getAgentId();
					paydto.setAgentId(agentId);
					payDao.insertAgent(paydto);
				}

				for (PayItemDTO dto : dtos) {
					dto.setPayId(String.valueOf(paydto.getId()));
					payItemDAO.insert(dto);
				}
				return SysCode.SUCCESS;
			}
		});
		return result;

	}
	
	
	public ReturnCode updatePayItem(String payId,List<PayItemDTO> dtos) throws PendingException{
		PayDTO paydto = new PayDTO();
		// 有控制权的
		ReturnCode result = forceTransactionTemplate.execute(new OutstandingTransactionCallback<ReturnCode>() {
			// 先调用账户系统，创建对应的代理商账户
			/**
			 * 此方法必须抛出PendingException异常
			 */
			@Override
			public ReturnCode doInTransaction(TransactionStatus status) throws PendingException {
				PayDTO paydto = payDao.selectOne(payId);
				
				payItemDAO.delete(String.valueOf(paydto.getId()));
				for (PayItemDTO dto : dtos) {
					dto.setPayId(String.valueOf(paydto.getId()));
					payItemDAO.insert(dto);
				}
				return SysCode.SUCCESS;
			}
		});
		return result;
	}

	public PageInfo queryByAgentId(int pageNo, int pageSize, String agentid) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<OrderVO> list = orderDao.queryByAgentId(agentid);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	public PageInfo queryByMerchantId(int pageNo, int pageSize, String merchantId) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<OrderVO> list = orderDao.queryByMerchantId(merchantId);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	public PageInfo query(int pageNo, int pageSize) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<OrderVO> list = orderDao.query();
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	public PayInfoDTO queryPay(String agentid, String merchantid) throws PendingException {
		PayInfoDTO dto = new PayInfoDTO();
		if (StringUtils.isBlank(merchantid)) {
			List<PayItemDTO> items = payItemDAO.selectByAgentID(agentid);
			dto.setItems(items);
			if(items.size() >0) {
				dto.setId(items.get(0).getPayId());
			}
			return dto;
		} else {
			List<PayItemDTO> items = payItemDAO.selectByMerchantID(merchantid);
			dto.setItems(items);
			if(items.size() >0) {
				dto.setId(items.get(0).getPayId());
			}
			return dto;
		}

	}

	public Amount countByAgentId(String agentid) {
		Amount amount = new Amount();
		amount.setOffLine(orderDao.countOffLineByAgentId(agentid));
		amount.setOnLine(orderDao.countOnLineByAgentId(agentid));
		return amount;
	}

	public Amount countByMerchantId(String merchantId) {
		Amount amount = new Amount();
		amount.setOffLine(orderDao.countOffLineByMerchantId(merchantId));
		amount.setOnLine(orderDao.countOnLineByMerchantId(merchantId));
		return amount;
	}

	public Amount countAll() {
		Amount amount = new Amount();
		amount.setOffLine(orderDao.countALLOffLine());
		amount.setOnLine(orderDao.countALLOnLine());
		return amount;
	}

	public PageInfo queryByReqPageDto(int pageNo, int pageSize, ReqPageDto dto) {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<OrderVO> list = orderDao.queryByReqPageDto(dto);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	

}
